package client;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.concurrent.TimeUnit;
import common.*;

//This is the bid or ask window
// which is a child of the round window.

public class SwingBidAsk extends JFrame {
	private static final long serialVersionUID = 4887270680651844032L;
	final private boolean isBid;
	final JTextField price = new JTextField("");
	final JTextField quantity = new JTextField("");
	final JTextField duration = new JTextField("");
	final JButton bidask = new JButton("");//(bid)?"Bid":"Ask");
	private enum Focus { PRICE, QUANTITY, DURATION };
	private Focus state;
	
	public SwingBidAsk ( String owner, boolean bid, Auction a, MessageSender s) {
		this(owner,bid,a.quantity,a.price, a.commodity,s);
	}
	public SwingBidAsk ( String owner, boolean bid, Commodity c, MessageSender s) {
		this(owner,bid,0,0,c,s);
	}
	public SwingBidAsk ( final String owner, boolean bid, int q, double p,final Commodity c, final MessageSender s) {
		isBid = bid;
		setTitle(((bid)?"Bid ":"Ask ")+ c.toString());
		this.setSize(200,400);
		JPanel jp = new JPanel();
		jp.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
		jp.setLayout(new GridLayout(4,2,10,10));
	
		this.setLocation(100,100);
		
		price.setText(p+"");
		quantity.setText(q+"");
		bidask.setText((isBid)?"Bid":"Ask");
		duration.setText(""+60*4);
		price.setPreferredSize(new Dimension(100,20));
		
		bidask.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e){
        		double pr = getPrice();
				int qnt = getQuant();
				long dur = getDuration();
				if (isBid) {
					s.send(new Bid(owner,c,pr,qnt,dur,TimeUnit.SECONDS));
				} else {
					s.send(new Ask(owner,c,pr,qnt,dur,TimeUnit.SECONDS));	
				}
			}
        });
		JLabel priceL = new JLabel("Price : ");
		JLabel quantL = new JLabel("Quantity : ");
		JLabel duratL = new JLabel("Time(sec): ");
		
		jp.add(priceL);
		jp.add(price);
		jp.add(quantL);
		jp.add(quantity);
		jp.add(duratL);
		jp.add(duration);
		
		jp.add(Box.createRigidArea(new Dimension(20, 0)));
		jp.add(bidask);
		this.add(jp);
		this.pack();
		this.validate();
		this.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );
	}
	public double getPrice() {
		try {
			double d =  Double.parseDouble(price.getText());
			double r = (100*d - Math.floor(100*d))/100;
			return d - r;
		
		} catch (Exception e) {
			return 0.0;
		}
	}
	public int getQuant() {
		try {
			return Integer.parseInt(quantity.getText());
		} catch (Exception e) {
			return -1;
		}
	}
	public long getDuration() {
		try {
			return Long.parseLong(duration.getText());
		} catch (Exception e) {
			return -1;
		}
	}
	//Used to make sure the focus for each field is maintained
	//throughout updates to other windows.
	public void storeFocus() {
		if (price.hasFocus()) state = Focus.PRICE;
		if (quantity.hasFocus()) state = Focus.QUANTITY;
		if (duration.hasFocus()) state = Focus.DURATION;
	}
	public void requestFocus() {
		super.requestFocus();
		if (state == Focus.PRICE) price.requestFocus();
		if (state == Focus.QUANTITY) quantity.requestFocus();
		if (state == Focus.DURATION) duration.requestFocus();
	}
}
